
#ifndef UTIL_MACRO_H_
#define UTIL_MACRO_H_
#include "../sleipner_config.h"
#ifdef _PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#endif //_PLATFORM_WIN32

// Flag test
#define	UTIL_SET_FLAG(Flag, Value)	Flag |= Value
#define	UTIL_CLR_FLAG(Flag, Value)	Flag  = Flag& ~Value
#define	UTIL_TST_FLAG(Flag, Value)	(Flag & Value)

// MIN / MAX
#define		UTIL_LOW(x, y)	((x < y) ? (x) : (y))
#define		UTIL_HIGH(x, y)	((x > y) ? (x) : (y))
#define		UTIL_CLAMP(x, min, max) { if (x < min) x = min; if (x > max) x = max; }

#define UTIL_RANGE(A, MIN, MAX) ((MIN <= A) && (A <= MAX))
#define UTIL_SUB_RANGE(A, MIN, MAX) ((MIN < A) && (A < MAX))
#define	BIG_FLOAT		(float)10e10

// logic
#define		UTIL_XOR(x, y)	((!(x)&&(y)) || ((x)&&!(y)))

// Clear and delete variables
#define	UTIL_SAFE_DELETE(x)		{	if (x) delete x; x=NULL;		}	// delete variable
#define UTIL_SAFE_DELETEA(x)	{	if (x) delete[] x; x=NULL;      }	// delete array
#define UTIL_SAFE_RELEASE(x)	{	if (x) x->Release(); x=NULL;    }	// release COM pointer

// UI
#define UTIL_UI_GETPANEL(Panel, PanelName) {Panel = ui::UIManager::Instance().GetPanel(PanelName); if (Panel == NULL) _LOG(MSG_ERROR, "Can't find UI Panel: " << PanelName);}
#define UTIL_UI_GETWIDGET(Widget, WidgetName, Panel) {if (Panel != NULL) {Widget = Panel->GetWidgetByName(WidgetName); if (Widget == NULL) _LOG(MSG_WARNING, "Can't find widget " << WidgetName << "in panel " << Panel->GetName());} else Widget = NULL;}

#define		ENABLE_BUFFER_TEST
#ifdef ENABLE_BUFFER_TEST
	#define UTIL_TEST_OVERRUN 0xC0FFEB01
	#define	CHECK_OVERRUN(VAR)   { assert(VAR == UTIL_TEST_OVERRUN); } 
	#define	SET_OVERRUN(VAR) { VAR = UTIL_TEST_OVERRUN; } 
	#define ADD_OVERRUN(VAR) unsigned long VAR;
	#define STATIC_OVERRUN(VAR) static unsigned long VAR = UTIL_TEST_OVERRUN; 
#else
	#define	CHECK_OVERRUN(VAR)   
	#define	SET_OVERRUN(VAR) 
	#define ADD_OVERRUN(VAR) 
	#define STATIC_OVERRUN(VAR) 
#endif

#endif  //UTIL_MACRO_H_